package com.zimmur.service.impl.zone;
import java.util.List;

import javax.annotation.Resource;  
  
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zimmur.common.AppModuleEnum;
import com.zimmur.common.IDWorkFactory;
import com.zimmur.dao.zone.ITopicDao;
import com.zimmur.dao.zone.ITopicPictureDao;
import com.zimmur.model.zone.Topic;
import com.zimmur.model.zone.TopicPicture;
import com.zimmur.service.zone.ITopicPictureService;
import com.zimmur.tools.DateUtils;
  
  
@Service("topicPictureService")  
public class TopicPictureServiceImpl implements ITopicPictureService {  
    @Resource  
    private ITopicPictureDao topicPictureDao;
    @Resource  
    private ITopicDao topicDao;

	@Override
	@Transactional
	public int insertTopicPicture(long tid,String pictures) {
		Integer resut=0;
		try {
			Integer timeDate =DateUtils.getUnixTimeByDate(DateUtils.getNow());
//		    List<TopicPicture> topicPictureList =new ArrayList<TopicPicture>(); 
	        /*获取图片（key）和备注(value)，格式为键值对*/
		    String[] sArr=pictures.split(",");
		    //TopicPicture topicPicture=null;
		    TopicPicture topicPicture=new TopicPicture();
		    if(sArr != null && sArr.length>0 && sArr[0] != ""){
			    for(String s:sArr){
			      String[] ss=s.split("=");
			      String key=ss[0];
			      String value=ss[1];
			      topicPicture.setImageUrl(key);
			      topicPicture.setMemo(value == null ? "":value);
			      topicPicture.setTopicId(tid);
			      topicPicture.setCreateDate(timeDate);
//	              topicPictureList.add(topicPicture);
			      topicPicture.setPictureId(IDWorkFactory.getID(AppModuleEnum.Zone));//不使用自增，自己创建ID
			      topicPictureDao.insertTopicPicture(topicPicture);
			      resut ++;
			    } 
		    }
		    //添加话题封面图
			TopicPicture topicPictures=new TopicPicture();
			topicPictures.setTopicId(topicPicture.getTopicId());
			TopicPicture topicPicture1=topicPictureDao.selectOneTopicPicture(topicPictures);
			if(topicPicture1 != null){
				Topic topic=new Topic();
				topic.setTopicId(topicPicture1.getTopicId());
				topic.setCoverImage(topicPicture1.getImageUrl());
				topicDao.updateStopicCoverImage(topic);
			}
		} catch (Exception e) {
			e.getMessage();
		}
		return resut;
	}

	@Override
	public List<TopicPicture> selectByTopicIdTopicPictureList(long topicId) {
		return topicPictureDao.selectByTopicIdTopicPictureList(topicId);
	}  
}  
